import 'dart:io';
import 'package:dio/dio.dart';
import 'package:loxen/api/http_util.dart';
import 'package:rxdart/rxdart.dart';

import 'http_util.dart';

class ApiService {
  ///获取最新app信息
  static Stream<Response> upGradeApp(bool isAndroid) {
    return Rx.defer(() => HttpUtil.get('/api/v1/public/apk_version',
        params: {'query': 'latest', 'package_type': isAndroid ? 'apk' : 'ios'}).asStream());
  }

  ///取消订单
  static Stream<Response> cancelOrder(String pk) {
    var params = {'is_cancel': true};
    return Rx.defer(() => HttpUtil.put(
          '/api/v1/payments/order/$pk',
          params: params,
          queryParameters: params,
        ).asStream());
  }

  ///业务员详情
  static Stream<Response> staffInfo() {
    return Rx.defer(() => HttpUtil.get('/api/v1/service/staff/info').asStream());
  }

  ///业务员备注信息
  static Stream<Response> orderRemark(String pk, String text) {
    return Rx.defer(() => HttpUtil.post('/api/v1/service/staff/remark', {
          'order_id': pk,
          'remark': text,
        }).asStream());
  }

  ///业务员下一步
  static Stream<Response> orderNext(String pk) {
    return Rx.defer(
        () => HttpUtil.put('/api/v1/service/staff/schedule', params: {'order_id': pk}).asStream());
  }

  ///确认订单完成
  static Stream<Response> orderOk(String pk) {
    return Rx.defer(() => HttpUtil.put('/api/v1/payments/order/$pk', params: {}).asStream());
  }

  ///日程表
  /// 0：全部
  /// 1：待备样
  /// 2：待送样
  /// 3：已完成
  static Stream<Response> schedule(String mode, {String page = '1', String pageSize = '2'}) {
    Map<String, dynamic> params = {
      'status': mode,
      'page': page,
      'page_size': pageSize,
    };
    return Rx.defer(
        () => HttpUtil.get('/api/v1/service/staff/schedule', params: params).asStream());
  }

  ///扫码登录
  static Stream<Response> qrLogin(String uid) {
    return Rx.defer(() => HttpUtil.post('/api/v1/user/scan/code', {'uid': uid}).asStream());
  }

  ///申请业务员
  static Stream<Response> applyStaff(String name, int centerId, {int departmentId}) {
    Map<String, dynamic> params = {'service_centre_id': centerId, 'real_name': name};
    if (departmentId != null) {
      params['department_id'] = departmentId;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/service/staff', params: params).asStream());
  }

  ///是否为业务员
  static Stream<Response> isStaff() {
    return Rx.defer(() => HttpUtil.get('/api/v1/service/is_staff').asStream());
  }

  ///订单详情
  static Stream<Response> orderDetail(String pk) {
    return Rx.defer(() => HttpUtil.get('/api/v1/payments/order/$pk').asStream());
  }

  ///获取订单列表
  static Stream<Response> orderList({String status, String page = '1', String pageSize = '4'}) {
    Map<String, dynamic> params = {
      'status': status,
      'page': page,
      'page_size': pageSize,
    };
    return Rx.defer(() => HttpUtil.get('/api/v1/payments/orders', params: params).asStream());
  }

  ///下单
  static Stream<Response> putOrder({
    String addressId,
    String mode,
    String hsIds,
    String serviceId,
    String startTime,
    String endTime,
    String remark,
  }) {
    Map params = {};
    params['mode'] = mode;
    params['hs_ids'] = hsIds;
    params['remark'] = remark;
    if (addressId != null) {
      params['address_id'] = addressId;
    }
    if (serviceId != null) {
      params['service_centre_id'] = serviceId;
    }
    if (startTime != null) {
      params['delivery_start_time'] = "$startTime:00";
      params['delivery_end_time'] = "$endTime:00";
    }
    return Rx.defer(() => HttpUtil.post('/api/v1/payments/orders', params).asStream());
  }

  ///服务i中心服务范围
  ///[id] 公司id
  static Stream<Response> operationLocations(String id) {
    return Rx.defer(() => HttpUtil.get('/api/v1/service/service_centre/range/$id').asStream());
  }

  ///服务中心列表
  static Stream<Response> operationList(String id, {String city}) {
    Map<String, dynamic> params = {'page_size': 50};
    if (city != null) {
      params = {'city': city};
    }
    return Rx.defer(
        () => HttpUtil.get('/api/v1/service/service_centre/$id', params: params).asStream());
  }

  ///删除样单
  static Stream<Response> deleteShopcar(String pks) {
    return Rx.defer(() => HttpUtil.delete('/api/v1/payments/shopcar/$pks').asStream());
  }

  ///样单总数
  static Stream<Response> shopcarNum() {
    return Rx.defer(() => HttpUtil.get('/api/v1/payments/shopcar/count').asStream());
  }

  ///查询样单列表
  static Stream<Response> shopcarList() {
    return Rx.defer(() => HttpUtil.get('/api/v1/payments/shopcar').asStream());
  }

  ///添加购物车
  static Stream<Response> shopcar(String pk) {
    return Rx.defer(() => HttpUtil.post('/api/v1/payments/shopcar/$pk', {}).asStream());
  }

  ///校验用户是否有初始密码
  static Stream<bool> checkPwd() {
    return Rx.defer(() => HttpUtil.get('/api/v1/user/account/password/0').asStream())
        .flatMap((value) {
      bool data;
      if (value.data is Map) {
        var v = value.data['data']['data'];
        if (v is bool) {
          data = v;
        }
      }
      return Stream.value(data);
    });
  }

  ///用户举报
  ///[type]
  //灵感：idea-article
  //知识：study-knowledge
  //书本：study-book
  //应用：study-case
  //评论：users-comment
  /// [content] 举报内容  [id] 举报ID [type] 内容类型  [reportType] 举报类型
  static Stream<Response> report(String content, String id, String type, String reportType) {
    Map params = {
      'content': content,
      'instance_id': id,
      'content_type': type,
      'report_type': reportType,
    };
    return Rx.defer(() => HttpUtil.post('/api/v1/user/report', params).asStream());
  }

  /// 极光推送提交rid /api/v1/user/account/jiguang_id/0
  /// [jiguang_id] 极光rid
  static Stream<Response> ridPost(String rid) {
    Map params = {'jiguang_id': rid};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/account/jiguang_id/0', params).asStream());
  }

  /// 极光推送删除rid /api/v1/user/account/jiguang_id/0
  static Stream<Response> ridDelete() {
    return Rx.defer(() => HttpUtil.delete('/api/v1/user/account/jiguang_id/0').asStream());
  }

  ///用户点赞 /api/v1/user/user_likes
  ///[userId] 用户id
  static Stream<Response> userLike(String userId, {bool delete = false}) {
    Map params = {'user_id': userId};
    if (delete) {
      return Rx.defer(() => HttpUtil.delete('/api/v1/user/user_likes', params: params).asStream());
    } else {
      return Rx.defer(() => HttpUtil.post('/api/v1/user/user_likes', params).asStream());
    }
  }

  ///统计阅读数
  ///[type] 统计类型  [id]内容id
  ///灵感：idea-article
  ///知识：study-knowledge
  ///书本：study-book
  ///应用：study-case
  static Stream<Response> statistics(String type, String id) {
    return Rx.defer(() => HttpUtil.get('/api/v1/statistic/$type/$id').asStream());
  }

  ///未登录状态忘记密码  /api/v1/user/forget
  ///[mobile]手机号  [password]密码  [code]验证码
  static Stream<Response> forget(String mobile, String password, String code) {
    Map<String, dynamic> params = {"mobile": mobile, "password": password, "code": code};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/forget', params).asStream());
  }

  ///登录状态忘记密码  /api/v1/user/account/password/0
  ///[password]密码  [newPassword]新密码
  static Stream<Response> password(String password, String newPassword) {
    Map<String, dynamic> params = {"password": password, "new_password": newPassword};
    return Rx.defer(
        () => HttpUtil.put('/api/v1/user/account/password/0', params: params).asStream());
  }

  ///企业认证 /api/v1/user/brand
  ///[user_id] 用户id
  static Stream<Response> business(
    FormData portrait,
  ) {
    return Rx.defer(() => HttpUtil.post('/api/v1/user/brand', portrait).asStream());
  }

  ///qq验证  /api/v1/user/qqusers
  ///[open_id]openid [nickname]nickname [access_token]token
  static Stream<Response> qqPermit(Map userData) {
    Map<String, dynamic> params;
    if (Platform.isIOS) {
      params = {
        "openid": userData['credential']['rawData']['openid'],
        "nickname": userData['rawData']['nickname'],
        "access_token": userData['credential']['rawData']['access_token'],
      };
    } else {
      params = {
        "openid": userData['userID'],
        "nickname": userData['nickname'],
        "access_token": userData['token'],
      };
    }
    return Rx.defer(() => HttpUtil.post('/api/v1/user/qqusers', params).asStream());
  }

  ///微信验证  /api/v1/user/wechatusers
  ///[open_id]openid [unionid]unionid [nickname]nickname
  ///[headimgurl]icon [sex]gender [province]province
  ///[language]'' [city]city [country]country [access_token]token
  static Stream<Response> wechatPermit(Map userData) {
    Map<String, dynamic> params;
    if (Platform.isIOS) {
      params = {
        "openid": userData['credential']['rawData']['openid'],
        "unionid": userData['credential']['rawData']['unionid'],
        "nickname": userData['rawData']['nickname'],
        "headimgurl": userData['rawData']['headimgurl'],
        "sex": userData['gender'],
        "province": userData['rawData']['province'],
        "language": userData['rawData']['language'],
        "city": userData['rawData']['city'],
        "country": userData['rawData']['country'],
        "access_token": userData['credential']['rawData']['access_token'],
      };
    } else {
      params = {
        "openid": userData['openid'],
        "unionid": userData['unionid'],
        "nickname": userData['nickname'],
        "headimgurl": userData['icon'],
        "sex": userData['gender'],
        "province": userData['province'],
        "language": '',
        "city": userData['city'],
        "country": userData['country'],
        "access_token": userData['token'],
      };
    }
    return Rx.defer(() => HttpUtil.post('/api/v1/user/wechatusers', params).asStream());
  }

  ///微博验证  /api/v1/user/weibousers
  ///[open_id]userID [unionid]unionid [nickname]nickname
  ///[headimgurl]icon [sex]gender [province]province
  ///[language]'' [city]city [country]country [access_token]token
  static Stream<Response> sinaPermit(Map userData) {
    Map<String, dynamic> params;
    if (Platform.isIOS) {
      params = {
        "openid": userData['uid'],
        "nickname": userData['nickname'],
        "headimgurl": userData['icon'],
        "sex": userData['gender'],
        "access_token": userData['credential']['token'],
      };
    } else {
      params = {
        "openid": userData['userID'],
        "nickname": userData['nickname'],
        "headimgurl": userData['icon'],
        "sex": userData['gender'],
        "access_token": userData['token'],
      };
    }

    return Rx.defer(() => HttpUtil.post('/api/v1/user/weibousers', params).asStream());
  }

  ///苹果验证  /api/v1/user/appleusers
  ///[openid]userID [family_name]unionid [given_name]nickname
  ///[email]icon [access_token]
  static Stream<Response> applePermit(Map userData) {
    Map<String, dynamic> params;
    print('1${userData}');
    print(userData['credential']['rawData']['fullName']);
    var family_name = userData['credential']['rawData']['fullName'] != null
        ? userData['credential']['rawData']['fullName']['familyName']
        : null;
    var given_name = userData['credential']['rawData']['fullName'] != null
        ? userData['credential']['rawData']['fullName']['givenName']
        : null;
    params = {
      "openid": userData['uid'],
      "family_name": family_name,
      "given_name": given_name,
      "email": userData['credential']['rawData']['email'],
      "access_token": userData['credential']['token'],
    };
    print(params);
    return Rx.defer(() => HttpUtil.post('/api/v1/user/appleusers', params).asStream());
  }

  ///第三方注册  /api/v1/user/mobile
  ///[platform]wechat||weibo [openid]openid  [password]密码  [mobile]手机号  [code]验证码
  static Stream<Response> thirdPartRegister(
      String platform, String openid, String mobile, String code) {
    Map<String, dynamic> params = {"open_id": openid, "mobile": mobile, "code": code};
    print('第三方登录$platform');
    return Rx.defer(() => HttpUtil.post('/api/v1/user/$platform', params).asStream());
  }

  ///第三方直接登陆
  ///[open_id]手机号
  static Stream<Response> thirdPartLogin(String platform, String openid) {
    Map<String, dynamic> params = {"open_id": openid};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/token/${platform}', params).asStream());
  }

  ///登陆  /api/v1/user/token/password
  ///[mobile]手机号 [password] 密码
  static Stream<Response> login(String mobile, String password) {
    Map<String, dynamic> params = {"mobile": mobile, "password": password};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/token/password', params).asStream());
  }

  ///验证码登陆  /api/v1/user/token/code
  ///[mobile]手机号 [code] 验证码
  static Stream<Response> logincode(String mobile, String code) {
    Map<String, dynamic> params = {"mobile": mobile, "code": code};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/token/code', params).asStream());
  }

  ///获取验证码  /api/v1/user/code
  ///[mobile]手机号 [action] 状态码
  static Stream<Response> code(String mobile, String action) {
    Map<String, dynamic> params = {"mobile": mobile, "action": action};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/code', params).asStream());
  }

  ///校验验证码  /api/v1/user/code
  ///[mobile]手机号 [action] 状态码   [code]验证码
  static Stream<Response> vacode(
    String mobile,
    String action,
    String code,
  ) {
    Map<String, dynamic> params = {"mobile": mobile, "action": action, "code": code};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/code', params).asStream());
  }

  ///手机注册  /api/v1/user/mobile
  ///[nickname]昵称  [password]密码  [mobile]手机号  [code]验证码 [is_brand]是否是企业用户
  static Stream<Response> register(
      String nickname, String password, String mobile, String code, bool is_brand) {
    Map<String, dynamic> params = {
      "nickname": nickname,
      "password": password,
      "mobile": mobile,
      "code": code,
      "is_brand": is_brand,
    };
    return Rx.defer(() => HttpUtil.post('/api/v1/user/mobile', params).asStream());
  }

  ///灵感分类  /api/v1/idea/category
  static Stream<Response> inspirationCategory() {
    return Rx.defer(() => HttpUtil.get('/api/v1/idea/category').asStream());
  }

  ///灵感风格 /api/v1/idea/style
  static Stream<Response> inspirationStyle() {
    return Rx.defer(() => HttpUtil.get('/api/v1/idea/style').asStream());
  }

  ///灵感列表 /api/v1/idea/article
  ///[category]类型分类   [page] 分页页码 [style] 风格id
  static Stream<Response> interceptListData(String category, int page, {int style}) {
    Map<String, dynamic> params = {'category': category, 'page': page};
    if (style != null) {
      params['style'] = style;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/idea/article', params: params).asStream());
  }

  ///灵感：idea-article
  ///知识：study-knowledge
  ///书本：study-book
  ///应用：study-case
  ///素材：material-product
  static Stream<Response> likeSomething(String type, String id) {
    Map<String, dynamic> params = {'instance_id': id};
    return Rx.defer(() => HttpUtil.post("/api/v1/user/likes/$type", params).asStream());
  }

  static Stream<Response> unLikeSomething(String type, String id) {
    return Rx.defer(() => HttpUtil.delete('/api/v1/user/likes/$type/$id').asStream());
  }

  ///灵感点赞 /api/v1/user/likes/idea-article
  ///[instanceId]文章ID
  static Stream<Response> likeArticle(String instanceId) {
    Map<String, dynamic> params = {'instance_id': instanceId};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/likes/idea-article', params).asStream());
  }

  ///灵感取消点赞 /api/v1/user/likes/idea-article
  ///[instanceId]文章ID
  static Stream<Response> unLikeArticle(String instanceId) {
    return Rx.defer(
        () => HttpUtil.delete('/api/v1/user/likes/idea-article/$instanceId').asStream());
  }

  ///素材点赞 /api/v1/user/likes/material-product
  ///[instanceId] 素材ID
  static Stream<Response> likeMaterial(int instanceId) {
    Map<String, dynamic> params = {"instance_id": instanceId};
    return Rx.defer(() => HttpUtil.post("/api/v1/user/likes/material-product", params).asStream());
  }

  ///素材取消点赞 /api/v1/user/likes/material-product
  ///[materialId] 素材ID
  static Stream<Response> unlikeMaterial(int id) {
    return Rx.defer(() => HttpUtil.delete("/api/v1/user/likes/material-product/$id").asStream());
  }

  ///灵感文章内容  /api/v1/idea/article/
  ///[id]文章id
  static Stream<Response> inspirationContent(String instanceId) {
    return Rx.defer(() => HttpUtil.get("/api/v1/idea/article/$instanceId").asStream());
  }

  ///灵感文章评论列表  /api/v1/comment/idea-article
  ///[instanceId]
  static Stream<Response> inspirationComment(String instanceId, String page, {String pageSize}) {
    Map<String, String> params = {
      'instance_id': instanceId,
      'page': page,
    };
    if (pageSize != null) {
      params['page_size'] = pageSize;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/comment/idea-article', params: params).asStream());
  }

  ///评论的子评论 /api/v1/sub_comment
  ///[parentComment]评论id  [page]分页
  static Stream<Response> commentList(String parentComment, String page, {String pageSize}) {
    Map<String, String> map = {'parent_comment': parentComment, 'page': page};
    if (pageSize != null) {
      map['page_size'] = pageSize;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/sub_comment', params: map).asStream());
  }

  ///评论点赞接口  /api/v1/user/comment_likes
  ///[comment] 评论id
  static Stream<Response> likeComment(String id) {
    Map<String, String> params = {'comment': id};
    return Rx.defer(() => HttpUtil.post("/api/v1/user/comment_likes", params).asStream());
  }

  ///评论取消点赞 /api/v1/user/comment_likes/72
  ///[comment] 评论id
  static Stream<Response> unLikeComment(String id) {
    return Rx.defer(() => HttpUtil.delete("/api/v1/user/comment_likes/$id").asStream());
  }

  ///灵感详情猜你喜欢  /api/v1/favourite/guest/article
  ///[category]分类id [id]灵感id
  static Stream<Response> likeInspiration(String category, String id) {
    Map<String, String> params = {'category': category, 'id': id};
    return Rx.defer(
        () => HttpUtil.get('/api/v1/favourite/guest/article', params: params).asStream());
  }

  ///删除文章评论 /api/v1/comment/{content-type}/{type-id}/{pk}
  ///[contentType] 文章类型  [typeId] 文章id   [id] 评论id
  static Stream<Response> deleteComment(String contentType, String typeId, String id) {
    return Rx.defer(() => HttpUtil.delete('/api/v1/comment/$contentType/$typeId/$id').asStream());
  }

  ///评论灵感文章  /api/v1/comment/idea-article
  ///[instance_id] 文章id  [parent_comment] 评论id [content] 评论内容 [previousComment]上级评论
  static Stream<Response> commentInspiration(
      String id, String content, String parentComment, String previousComment) {
    Map<String, String> params = {
      'instance_id': id,
      'parent_comment': parentComment,
      'content': content,
      'previous_comment': previousComment
    };
    return Rx.defer(() => HttpUtil.post("/api/v1/comment/idea-article", params).asStream());
  }

  ///灵感文章搜索  /api/v1/idea/article
  ///[search] 搜索内容 [page] 内容页码
  static Stream<Response> searchInspiration(String search, String page) {
    Map<String, String> params = {'search': search, 'page': page};
    return Rx.defer(() => HttpUtil.get('/api/v1/idea/article', params: params).asStream());
  }

  ///素材首页 /api/v1/material/index
  static Stream<Response> materialIndex() {
    return Rx.defer(() => HttpUtil.get("/api/v1/material/index").asStream());
  }

  ///素材列表内容  /api/v1/material/product
  /// [category] 分类id [search] 搜索内容 [page] 分页页码
  static Stream<Response> materialProduct({String category, String search, String page = '1',String pageSize}) {
    Map<String, String> params = {
      'page': page,
    };
    if (category != null) {
      params['category'] = category;
    }
    if (search != null) {
      params['search'] = search;
    }
    if(pageSize !=null) {
      params['page_size'] = pageSize;
    }
    return Rx.defer(() => HttpUtil.get("/api/v1/material/product", params: params).asStream());
  }

  ///素材分类  /api/v1/material/category
  static Stream<Response> materialCategory() {
    return Rx.defer(() => HttpUtil.get('/api/v1/material/category').asStream());
  }

  ///素材详情 /api/v1/material/product
  ///[id]素材id
  static Stream<Response> materialInfo(int id) {
    return Rx.defer(() => HttpUtil.get("/api/v1/material/product/$id").asStream());
  }

  ///实样 /api/v1/payments/orders
  ///[addressId]收件地址id [hsId]花色id [ybgg]样板规格
  ///[mccl]面层材料 [bmjl]表面肌理 [remark]备注
  static Stream<Response> paymentOrder(
      int addressId, int hsId, String ybgg, String mccl, String bmjl, String remark) {
    Map<String, dynamic> params = {
      'address_id': addressId,
      'hs_id': hsId,
      'size': ybgg,
      'layer': mccl,
      'biaomianjili': bmjl,
      'remark': remark
    };
    return Rx.defer(() => HttpUtil.post('/api/v1/payments/orders', params).asStream());
  }

  /*****************************学习模块api***********************************/

  ///学习导航栏分类  /api/v1/study/category/knowledge
  static Stream<Response> studySubCategory(String type) {
    return Rx.defer(() => HttpUtil.get('/api/v1/study/category/$type').asStream());
  }

  ///学习分类  /api/v1/study/knowledge||case
  static Stream<Response> studyList(
    String type,
    String category,
    int page,
  ) {
    Map<String, dynamic> params = {'category': category, 'page': page};
    return Rx.defer(() => HttpUtil.get('/api/v1/study/$type', params: params).asStream());
  }

  ///学习 详情猜你喜欢  /api/v1/favourite/guest/article
  ///[category]分类id [id]灵感id
  static Stream<Response> guessLikeStudy(String type, String category, String id) {
    Map<String, String> params = {'category': category, 'id': id};
    return Rx.defer(() => HttpUtil.get('/api/v1/favourite/guest/$type', params: params).asStream());
  }

  ///学习点赞 /api/v1/user/likes/study-knowledge||study-case
  ///[instanceId]文章ID
  static Stream<Response> likeStudy(String instanceId, String type) {
    Map<String, dynamic> params = {'instance_id': instanceId};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/likes/study-$type', params).asStream());
  }

  ///学习取消点赞 /api/v1/user/likes/study-knowledge||study-case
  ///[instanceId]文章ID
  static Stream<Response> unLikeStudy(String instanceId, String type) {
    return Rx.defer(() => HttpUtil.delete('/api/v1/user/likes/study-$type/$instanceId').asStream());
  }

  ///学习文章内容  /api/v1/study/knowledge||case
  ///[id]文章id
  static Stream<Response> studyContent(String instanceId, String type) {
    return Rx.defer(() => HttpUtil.get("/api/v1/study/$type/$instanceId").asStream());
  }

  ///学习文章评论列表  /api/v1/comment/study-knowledge
  ///[instanceId]
  static Stream<Response> studyComment(String instanceId, String page, {String pageSize}) {
    Map<String, String> params = {
      'instance_id': instanceId,
      'page': page,
    };
    if (pageSize != null) {
      params['page_size'] = pageSize;
    }
    return Rx.defer(
        () => HttpUtil.get('/api/v1/comment/study-knowledge', params: params).asStream());
  }

  ///学习文章搜索  /api/v1/study/knowledge || case
  ///[search] 搜索内容 [page] 内容页码
  static Stream<Response> searchStudy(String type, String search, String page) {
    Map<String, String> params = {'search': search, 'page': page};
    return Rx.defer(() => HttpUtil.get('/api/v1/study/$type', params: params).asStream());
  }

  ///评论知识文章  /api/v1/comment/study-knowledge
  ///[instance_id] 文章id  [parent_comment] 评论id [content] 评论内容
  static Stream<Response> commentStudy(String id, String content,
      {String parent, String previousComment}) {
    Map<String, String> params = {
      'instance_id': id,
      'parent_comment': parent,
      'content': content,
      'previous_comment': previousComment
    };
    return Rx.defer(() => HttpUtil.post("/api/v1/comment/study-knowledge", params).asStream());
  }

  ///删除收货地址
  ///[id]地址id
  static Stream<Response> deleteAddress(String id) {
    return Rx.defer(() => HttpUtil.delete('/api/v1/payments/address/$id').asStream());
  }

  ///收货地址列表  /api/v1/payments/address
  static Stream<Response> getAddress({String page = '1', String pageSize = '10'}) {
    return Rx.defer(() =>
        HttpUtil.get('/api/v1/payments/address', params: {'page': page, 'page_size': pageSize})
            .asStream());
  }

  ///添加收货地址 /api/v1/user/address?user=0
  ///[signerName]收件人 [signerMobile] 手机号
  ///[signerSex]性别 [province] 省
  ///[city]市 [district] 区 [address]详细地址
  static Stream<Response> newAddress(String signerName, String signerMobile, String province,
      String city, String district, String address, bool defaultAddress, String lat, String lng) {
    Map<String, dynamic> params = {
      'signer_name': signerName,
      'signer_mobile': signerMobile,
      'province': province,
      'city': city,
      'district': district,
      'address': address,
      'default_addr': defaultAddress,
      'lat': lat,
      'lng': lng,
    };
    return Rx.defer(() => HttpUtil.post("/api/v1/payments/address", params).asStream());
  }

  ///修改收货地址 /api/v1/user/address/24
  ///  ///[signerName]收件人 [signerMobile] 手机号
  ///[signerSex]性别 [province] 省
  ///[city]市 [district] 区 [address]详细地址
  static Stream<Response> upAddress(
    String id,
    String signerName,
    String signerMobile,
    String province,
    String city,
    String district,
    String address,
    bool defaultAddress,
    String lat,
    String lng,
  ) {
    Map<String, dynamic> params = {
      'signer_name': signerName,
      'signer_mobile': signerMobile,
      'province': province,
      'city': city,
      'district': district,
      'address': address,
      'default_addr': defaultAddress,
      'lat': lat,
      'lng': lng,
    };
    return Rx.defer(() => HttpUtil.put("/api/v1/payments/address/$id", params: params).asStream());
  }

  ///大师桌文件夹 /api/v1/favourite/folders/index
  static Stream<Response> masterIndex({String userId}) {
    if (userId != null) {
      return Rx.defer(() => HttpUtil.get('/api/v1/favourite/folders/index',
          params: {'type': 'master', 'user_id': userId}).asStream());
    } else {
      return Rx.defer(() => HttpUtil.get('/api/v1/favourite/folders/index').asStream());
    }
  }

  ///文件夹修改名称 /api/v1/favourite/folders/
  ///[id] 文件夹id [name]需要修改的新名字
  static Stream<Response> reNameFile(String id, String name) {
    Map<String, dynamic> params = {'name': name};
    return Rx.defer(() => HttpUtil.put("/api/v1/favourite/folders/$id", params: params).asStream());
  }

  ///修改文件夹隐藏
  ///[id] 文件夹id   [hide]隐藏
  static Stream<Response> fileHide(Map data) {
    Map params = {'instance': data};
    return Rx.defer(
        () => HttpUtil.put('/api/v1/favourite/folders/change', params: params).asStream());
  }

  ///灵感文件详情 /api/v1/favourite/idea-article
  ///[id] 文件夹id
  static Stream<Response> fileInspirationInfo(String id, {String page = '1', String userId}) {
    Map<String, String> params = {'folder': id, 'page': page};
    if (userId != null) {
      params['user_id'] = userId;
    }
    return Rx.defer(
        () => HttpUtil.get("/api/v1/favourite/idea-article", params: params).asStream());
  }

  ///删除收藏内容
  ///[ids] 内容id  [type] 数据类型  [folderId]文件夹ID
  static Stream<Response> deleteCollection(String ids, String type, String folderId) {
    return Rx.defer(() =>
        HttpUtil.delete('/api/v1/favourite/$type/$ids', params: {'folder_id': folderId})
            .asStream());
  }

  ///收藏内容详情列表
  ///[id] 文件夹id  [type] 文件夹类型
  static Stream<Response> fileInfo(String id, String type, {String page = '1', String userId}) {
    Map<String, String> params = {'folder': id, 'page': page};
    if (userId != null) {
      params['user_id'] = userId;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/favourite/$type', params: params).asStream());
  }

  ///素材收藏文件夹详情 /api/v1/favourite/material-product
  ///[id] 文件夹id
  static Stream<Response> fileMaterialInfo(String id, {String page = '1', String userId}) {
    Map<String, String> params = {'folder': id, 'page': page};
    if (userId != null) {
      params['user_id'] = userId;
    }
    return Rx.defer(
        () => HttpUtil.get("/api/v1/favourite/material-product", params: params).asStream());
  }

  ///灵感图片文件夹详情 /api/v1/favourite/idea-picture
  ///[id] 文件夹id
  static Stream<Response> filePictureInfo(String id, {String page = '1', String userId}) {
    Map<String, String> params = {'folder': id, 'page': page};
    if (userId != null) {
      params['user_id'] = userId;
    }
    return Rx.defer(
        () => HttpUtil.get("/api/v1/favourite/idea-picture", params: params).asStream());
  }

  ///获取文件夹列表  /api/v1/favourite/folders
  /// [type]  文件类型  [page]分页
  //    ('1', '灵感'),
  //    ('2', '素材'),
  //    ('3', '采集'),
  //    ('4', '知识'),
  //    ('5', '应用'),
  //    ('6', '书本'),
  static Stream<Response> fileList(String type, String page, String userId, String pageSize) {
    Map<String, String> params = {'type': type, 'page': page};
    if (userId != null) {
      params['user_id'] = userId;
    }
    if (pageSize != null) {
      params['page_size'] = pageSize;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/favourite/folders', params: params).asStream());
  }

  ///删除文件夹  /api/v1/favourite/folders/delete/
  ///[ids] 文件夹id
  static Stream<Response> deleteFiles(String ids) {
    return Rx.defer(() => HttpUtil.delete('/api/v1/favourite/folders/delete/$ids').asStream());
  }

  ///创建文件夹 /api/v1/favourite/folders
  ///[name] 文件夹名称 [type] 文件夹类型
  static Stream<Response> createFile(String name, String type, bool isHide) {
    return Rx.defer(() =>
        HttpUtil.post("/api/v1/favourite/folders", {'name': name, 'type': type, 'is_hide': isHide})
            .asStream());
  }

  ///收藏内容 /api/v1/favourite/idea-article
  ///灵感：idea-article
  ///知识：study-knowledge
  ///采集：idea-picture
  ///书本：study-book
  ///应用：study-case
  ///素材：material-product
  ///[folder_id]文件夹id [instance_id]内容id
  static Stream<Response> collection(String type, String fileId,
      {String instanceId, String cover}) {
    Map params = {'folder_id': fileId};
    if (instanceId != null) {
      params['instance_id'] = instanceId;
    } else {
      params['cover'] = cover;
    }
    return Rx.defer(() => HttpUtil.post("/api/v1/favourite/$type", params).asStream());
  }

  ///取消收藏 /api/v1/favourite/idea-article/
  ///[id] 内容id
  static Stream<Response> unCollection(String type, String id) {
    return Rx.defer(() => HttpUtil.delete("/api/v1/favourite/$type/$id").asStream());
  }

  /*****************************我的菜单api***********************************/

  ///我的信息 /api/v1/user/index/0
  static Stream<Response> userInfoDetail(String userId) {
    return Rx.defer(
        () => HttpUtil.get('/api/v1/user/index/0', params: {"user_id": userId}).asStream());
  }

  ///我的通知消息 api/v1/user/notification?user=0
  ///[userId] 用户id  [page] 分页
  static Stream<Response> userMsgs({String userId = '0', String page = '1'}) {
    Map<String, String> params = {'user': userId, 'page': page};
    return Rx.defer(() => HttpUtil.get('/api/v1/user/notification', params: params).asStream());
  }

  // 修改我的背景 api/v1/user/account/background/0
  static Stream<Response> modifyBackground(
    FormData portrait,
  ) {
    return Rx.defer(
        () => HttpUtil.put("/api/v1/user/account/background/0", params: portrait).asStream());
  }

  ///账号信息 /api/v1/user/account/info/0
  static Stream<Response> userInfoEdit(String userId) {
    return Rx.defer(
        () => HttpUtil.get('/api/v1/user/account/info/0', params: {'user_id': userId}).asStream());
  }

  ///修改我的昵称 /api/v1/user/account/nickname/0
  ///[nickname] 昵称
  static Stream<Response> modifyNickname(
    String nickname,
  ) {
    Map<String, dynamic> params = {'nickname': nickname};
    return Rx.defer(
        () => HttpUtil.put("/api/v1/user/account/nickname/0", params: params).asStream());
  }

  ///修改我的头像 /api/v1/user/account/nickname/0
  ///[portrait] 头像
  static Stream<Response> modifyPortrait(
    FormData portrait,
  ) {
    return Rx.defer(
        () => HttpUtil.put("/api/v1/user/account/portrait/0", params: portrait).asStream());
  }

  ///修改我的信息 /api/v1/user/account/info/0
  ///[company]公司 [position] 职位 [email] 邮箱
  ///[sign]签名 [city] 城市 [sexual]性别
  static Stream<Response> modifyUserInfo(
    String company,
    String position,
    String email,
    String sign,
    String city,
    String sexual,
  ) {
    Map<String, dynamic> params = {
      'company': company,
      'position': position,
      'email': email,
      'sign': sign,
      'city': city,
      'sexual': sexual,
    };
    return Rx.defer(() => HttpUtil.put("/api/v1/user/account/info/0", params: params).asStream());
  }

  ///我的学习-知识和应用 /api/v1/study/$type?folder=x
  ///[type]类型(知识或者应用)[folder]文件夹id [page]分页
  static Stream<Response> mineStudyTypeList(String type, int folder, {String page = '1'}) {
    Map<String, Object> params = {'folder': folder, 'page': page};
    return Rx.defer(() => HttpUtil.get('/api/v1/study/$type', params: params).asStream());
  }

  ///我的作品 /api/v1/user/article
  ///[userId] 用户id  [page] 分页
  static Stream<Response> userWorks(String userId, String page, String pageSize) {
    Map<String, String> params = {'user_id': userId, 'page': page};
    if (pageSize != null) {
      params['page_size'] = pageSize;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/user/article', params: params).asStream());
  }

  ///我的订单 /api/v1/payments/orders?user=0
  ///[userId] 用户id  [page] 分页
  static Stream<Response> userOrders(
      {String page = '1', String userId = '0', String limit = '10'}) {
    Map<String, String> params = {'user': userId, 'page': page, 'limit': limit};
    return Rx.defer(() => HttpUtil.get('/api/v1/payments/orders', params: params).asStream());
  }

  // 我的收样地址 /api/v1/user/address?user=0
  static Stream<Response> userAddress() {
    return Rx.defer(() => HttpUtil.get('/api/v1/user/address?user=0').asStream());
  }

  ///我关注的人 /api/v1/user/follow
  ///[userId] 用户id  [page] 分页
  static Stream<Response> userFollowers(String userId, String page, String pageSize) {
    Map<String, String> params = {'user_id': userId, 'page': page};
    if (pageSize != null) {
      params['page_size'] = pageSize;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/user/follow', params: params).asStream());
  }

  ///素材发送邮箱
  ///[email]邮箱   [id]素材ID
  static Stream<Response> sendEmail(String email, String id) {
    Map params = {'email': email, 'hs_id': id};
    return Rx.defer(() => HttpUtil.post("/api/v1/payments/email", params).asStream());
  }

  ///用户关注 /api/v1/user/follow
  ///[id] 用户id
  static Stream<Response> followUser(String id) {
    Map<String, String> params = {'user_id': id};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/follow', params).asStream());
  }

  ///取消用户关注 /api/v1/user/follow
  ///[id] 用户id
  static Stream<Response> cancelFollow(String id) {
    return Rx.defer(() => HttpUtil.delete("/api/v1/user/follow/$id").asStream());
  }

  ///我赞过的人 /api/v1/user/likes?user=0
  ///[userId] 用户id  [page] 分页
  static Stream<Response> userLikes(String userId, String page, String pageSize) {
    Map<String, String> params = {'user_id': userId, 'page': page};
    if (pageSize != null) {
      params['page_size'] = pageSize;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/user/likes', params: params).asStream());
  }

  // 我赞过的-取消点赞
  static Stream<Response> cancelLike(String type, String id) {
    return Rx.defer(() => HttpUtil.delete("/api/v1/user/likes/$type/$id").asStream());
  }

  ///我的评论 /api/v1/user/comment?user=0
  ///[userId] 用户id  [page] 分页
  static Stream<Response> userComments(String userId, String page, String pageSize) {
    Map<String, String> params = {'user_id': userId, 'page': page};
    if (pageSize != null) {
      params['page_size'] = pageSize;
    }
    return Rx.defer(() => HttpUtil.get('/api/v1/user/comment', params: params).asStream());
  }

  ///获取用户隐私设置
  static Stream<Response> userPrivacy() {
    return Rx.defer(() => HttpUtil.get("/api/v1/user/privacy").asStream());
  }

  ///设置用户隐私权限
  ///[comment] 评论   [likes] 点赞
  static Stream<Response> setUserPrivacy(bool comment, bool likes) {
    Map params = {};
    if (comment != null) {
      params['comment'] = comment;
    }
    if (likes != null) {
      params['likes'] = likes;
    }
    return Rx.defer(() => HttpUtil.put('/api/v1/user/privacy', params: params).asStream());
  }

  /*****************************我的_设置api***********************************/

  ///用户意见反馈 /api/v1/user/feedback
  ///[content] 意见反馈内容
  static Stream<Response> feedback(String content) {
    Map<String, String> params = {'content': content};
    return Rx.defer(() => HttpUtil.post('/api/v1/user/feedback', params).asStream());
  }
}
